using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System.IO;
using HiCore.WebApi.Example.DomainEvents;
using HiCore.WebApi.Example.Repository;
using HiCore.Extensions.DependencyInjection;
using HiCore.Extensions.EventBus.Kafka;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Http;

namespace HiCore.WebApi.Producer
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddHttpContextAccessor();

            services.AddControllers();
            services.AddHealthChecks();
            services.AddHttpClient();

            services.AddKafkaEventBus();
            services.AddMyDbContextRepository(Configuration);


            services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo()
                {
                    Title = Configuration["Apollo:AppId"]
                });
            });
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();

                app.UseSwagger();
                app.UseSwaggerUI(options =>
                {
                    options.SwaggerEndpoint("/swagger/v1/swagger.json", "WebApi");
                    options.DocExpansion(Swashbuckle.AspNetCore.SwaggerUI.DocExpansion.None);
                });
            }

            app.UseUnitOfWorkDomainEvent(domainEvent =>
                _ = app.ApplicationServices.GetRequiredService<IKafkaProducerFactory>().PublishAsync((IKafkaMessage)domainEvent)
            );

            app.UseRouting();

          
            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapHealthChecks("/health", new HealthCheckOptions()
                {
                    ResponseWriter = (context, report) =>
                    {
                        return context.Response.WriteAsJsonAsync(new
                        {
                            Status = report.Status.ToString(),
                            Env = env.EnvironmentName,
                            AppId = Configuration?["Apollo:AppId"],
                            ApolloEnv = Configuration?["Apollo:Env"]
                        });
                    }
                });
            });
        }
    }
}
